﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TotalSystems.DataAccess;
using TotalSystems.Business.Public;
using TotalSystems.Business.Entitys.Store;

namespace TotalSystems.Business.Store
{
    public class StuffOrderRepository
    {
        TSContext _context = new TSContext();
        FinancialYearRepository financialYearRepository = new Public.FinancialYearRepository();

        #region Select
        public IEnumerable<StuffOrderWrapper> GetDegreeImportantStuff(StuffOrderWrapper stuffOrderWrapper)
        {
            var q = _context.STR_StoreDocument.Join(_context.PUB_Customer, sd => sd.PUB_Customer_ID_fk, c => c.PUB_Customer_ID, (sd, c) => new { sd, c })
                .Join(_context.STR_Store, j => j.sd.STR_Store_ID_fk, s => s.STR_Store_ID, (j, s) => new { j, s })
                .Join(_context.WOP_WorkPermission, j1 => j1.j.sd.WOP_Wp_ID_fk, wp => wp.WOP_Wp_ID, (j1, wp) => new { j1, wp })
                .Join(_context.PUB_Location, j2 => j2.j1.j.sd.PUB_Location_ID_fk, l => l.PUB_Location_ID, (j2, l) => new { j2, l })
                .Join(_context.ACC_AcountingDocument, j3 => j3.j2.j1.j.sd.ACC_Ad_Consistency_Id_fk, ad => ad.ACC_Ad_Id, (j3, ad) => new { j3, ad })
                .Join(_context.AGM_Agreement, j4 => j4.j3.j2.j1.j.sd.AGM_Agreement_ID_fk, agr => agr.AGM_Agreement_ID, (j4, agr) => new { j4, agr })
                .Join(_context.VEP_VendProduct, j5 => j5.j4.j3.j2.j1.j.sd.VEP_Vd_ID_fk, vp => vp.VEP_Vd_ID, (j5, vp) => new { j5, vp })
                .Join(_context.PUB_Department, j6 => j6.j5.j4.j3.j2.j1.j.sd.PUB_Department_ID_fk, d => d.PUB_Department_ID, (j6, d) => new { j6, d })
                .Join(_context.STR_CargoPaper, j7 => j7.j6.j5.j4.j3.j2.j1.j.sd.STR_CargoPaper_ID_fk, cp => cp.STR_CargoPaper_ID, (j7, cp) => new { j7, cp })
                .Join(_context.PUB_User, j8 => j8.j7.j6.j5.j4.j3.j2.j1.j.sd.PUB_User_ID_fk, u => u.PUB_User_ID, (j8, u) => new { j8, u })
                .Join(_context.ACC_FinancialYear, j9 => j9.j8.j7.j6.j5.j4.j3.j2.j1.j.sd.ACC_Fy_ID_fk, fy => fy.ACC_Fy_ID, (j9, fy) => new { j9, fy });
            var q1 = q.Select(x => new StuffOrderWrapper() 
            {
                ACC_Ad_Consistency_Name = x.j9.j8.j7.j6.j5.j4.ad.ACC_Ad_Name,
                PUB_Customer_Name = x.j9.j8.j7.j6.j5.j4.j3.j2.j1.j.c.PUB_Customer_Title

            });
            return q1;
        }

        #endregion

        #region Insert

        public void InsertDegreeImportantStuff(STR_DegreeImportantStuff sTR_DegreeImportantStuff)
        {
            sTR_DegreeImportantStuff.ACC_Fy_ID_fk = financialYearRepository.GetACC_FinancialYearActive().Select(x => x.ACC_Fy_ID_fk).First();
            _context.AddToSTR_DegreeImportantStuff(sTR_DegreeImportantStuff);
            _context.SaveChanges();
        }

        #endregion

        #region Update

        public void UpdateDegreeImportantStuff(STR_DegreeImportantStuff sTR_DegreeImportantStuff)
        {
            var q = _context.STR_DegreeImportantStuff.FirstOrDefault(x => x.STR_Dis_ID == sTR_DegreeImportantStuff.STR_Dis_ID);
            q.STR_Dis_Name = sTR_DegreeImportantStuff.STR_Dis_Name;
            _context.STR_DegreeImportantStuff.ApplyCurrentValues(q);
            _context.SaveChanges();
        }

        #endregion
    }
}
